﻿// 
// Copyright (c) 2013 Jason Bell
// 
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
// 

using System;

using Gharen.GLCore;

namespace ExampleDraw
{
    class Box
        : IDisposable
    {
        GLES30 gl;

        Buffer<Vertex> buffer;
        VertexArray vertexArray;
        Texture texture;
        int vertexCount;

        public Box(GLES30 gl, float xsize, float ysize, float zsize)
        {
            this.gl = gl;

            float x = xsize / 2;
            float y = ysize / 2;
            float z = zsize / 2;

            Vertex[] vertices = 
            {
                new Vertex(new Vector3(-x, y, z), new Vector2(0, 1), new Vector3(0, 0, 1)),
                new Vertex(new Vector3(-x, -y, z), new Vector2(0, 0), new Vector3(0, 0, 1)),
                new Vertex(new Vector3(x, -y, z), new Vector2(1, 0), new Vector3(0, 0, 1)),
                new Vertex(new Vector3(x, y, z), new Vector2(1,1), new Vector3(0, 0, 1)),
                new Vertex(new Vector3(-x, y, z), new Vector2(0, 1), new Vector3(0, 0, 1)),
                new Vertex(new Vector3(x, -y, z), new Vector2(1, 0), new Vector3(0, 0, 1)),

                new Vertex(new Vector3(-x, y, -z), new Vector2(0, 1), new Vector3(0, 0, -1)),
                new Vertex(new Vector3(-x, -y, -z), new Vector2(0, 0), new Vector3(0, 0, -1)),
                new Vertex(new Vector3(x, -y, -z), new Vector2(1, 0), new Vector3(0, 0, -1)),
                new Vertex(new Vector3(x, y, -z), new Vector2(1,1), new Vector3(0, 0, -1)),
                new Vertex(new Vector3(-x, y, -z), new Vector2(0, 1), new Vector3(0, 0, -1)),
                new Vertex(new Vector3(x, -y, -z), new Vector2(1, 0), new Vector3(0, 0, -1)),

                new Vertex(new Vector3(x, y, z), new Vector2(0, 1), new Vector3(1, 0, 0)),
                new Vertex(new Vector3(x, -y, z), new Vector2(0, 0), new Vector3(1, 0, 0)),
                new Vertex(new Vector3(x, -y, -z), new Vector2(1, 0), new Vector3(1, 0, 0)),
                new Vertex(new Vector3(x, -y, -z), new Vector2(1, 0), new Vector3(1, 0, 0)),
                new Vertex(new Vector3(x, y, -z), new Vector2(1, 1), new Vector3(1, 0, 0)),
                new Vertex(new Vector3(x, y, z), new Vector2(0, 1), new Vector3(1, 0, 0)),

                new Vertex(new Vector3(-x, y, z), new Vector2(0, 1), new Vector3(-1, 0, 0)),
                new Vertex(new Vector3(-x, -y, z), new Vector2(0, 0), new Vector3(-1, 0, 0)),
                new Vertex(new Vector3(-x, -y, -z), new Vector2(1, 0), new Vector3(-1, 0, 0)),
                new Vertex(new Vector3(-x, -y, -z), new Vector2(1, 0), new Vector3(-1, 0, 0)),
                new Vertex(new Vector3(-x, y, -z), new Vector2(1, 1), new Vector3(-1, 0, 0)),
                new Vertex(new Vector3(-x, y, z), new Vector2(0, 1), new Vector3(-1, 0, 0)),

                new Vertex(new Vector3(-x, y, -z), new Vector2(0, 0), new Vector3(0, 1, 0)),
                new Vertex(new Vector3(-x, y, z), new Vector2(0, 1), new Vector3(0, 1, 0)),
                new Vertex(new Vector3(x, y, z), new Vector2(1, 1), new Vector3(0, 1, 0)),
                new Vertex(new Vector3(x, y, -z), new Vector2(1, 0), new Vector3(0, 1, 0)),
                new Vertex(new Vector3(-x, y, -z), new Vector2(0,0), new Vector3(0, 1, 0)),
                new Vertex(new Vector3(x, y, z), new Vector2(1, 1), new Vector3(0, 1, 0)),

                new Vertex(new Vector3(-x, -y, -z), new Vector2(0, 0), new Vector3(0, -1, 0)),
                new Vertex(new Vector3(-x, -y, z), new Vector2(0, 1), new Vector3(0, -1, 0)),
                new Vertex(new Vector3(x, -y, z), new Vector2(1, 1), new Vector3(0, -1, 0)),
                new Vertex(new Vector3(x, -y, -z), new Vector2(1, 0), new Vector3(0, -1, 0)),
                new Vertex(new Vector3(-x, -y, -z), new Vector2(0, 0), new Vector3(0, -1, 0)),
                new Vertex(new Vector3(x, -y, z), new Vector2(1, 1), new Vector3(0, -1, 0))
            };
            vertexCount = vertices.Length;

            buffer = new Buffer<Vertex>(gl, gl.GL_ARRAY_BUFFER, vertices, Vertex.SizeInBytes);

            vertexArray = new VertexArray(gl, buffer);
            

            texture = new Texture(gl, "Content/wood.png");
        }

        public void Dispose()
        {
            this.texture.Dispose();
            this.vertexArray.Dispose();
            this.buffer.Dispose();
        }

        public void Render(int shadowTexture)
        {

            vertexArray.Bind();

            gl.ActiveTexture(gl.GL_TEXTURE0);

            //if(texture != 0)
            //    gl.BindTexture(gl.TextureTarget.Texture2D, texture);
            //else
            this.texture.Bind();

            gl.ActiveTexture(gl.GL_TEXTURE1);
            if (shadowTexture != 0)
            {

                gl.BindTexture(gl.GL_TEXTURE_2D, shadowTexture);
            }
            else
                gl.BindTexture(gl.GL_TEXTURE_2D, 0);
                

            gl.DrawArrays(gl.GL_TRIANGLES, 0, vertexCount);
        }
    }
}
